#!/usr/bin/env python
"""
/* radar_edbus.py - it's a radar UI game based on diversity-daemon 
 *
 * Copyright 2009 
 * Authored by Erin Yueh <erinyueh@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */
"""

import sys
import dbus
import e_dbus
import ecore
import dbus.service

class DBUS(dbus.service.Object):

 def __init__(self, r=0.1):
	self.mainloop = e_dbus.DBusEcoreMainLoop()
	self.bus = dbus.SessionBus(mainloop=self.mainloop)

	# World
	self.world = dbus.Interface(
			self.bus.get_object('org.openmoko.Diversity',
					'/org/openmoko/Diversity/world'),
			'org.openmoko.Diversity.World'
			)
	self.bard_path = self.world.GetSelf()

    # add fake examples
	self.tag_path = self.world.TagAdd(25.0, 10.0, "Willie")
	self.tag_path = self.world.TagAdd(5.0, -15.0, "Julian")
	self.tag_path = self.world.TagAdd(-20.1, 30.0, "Tick")
	self.tag_path = self.world.TagAdd(-40.1, -30.0, "Jeremy")
        
	# Object [bard]
	self.object_bard = dbus.Interface(
			self.bus.get_object('org.openmoko.Diversity', 
						self.bard_path),
			'org.openmoko.Diversity.Object'
			)
	loc = self.object_bard.GeometryGet()
	self.geometry_cord = (float(loc[0]), float(loc[1]))

    # register a callback for signal GeometryChanged
	self.object_bard.connect_to_signal("GeometryChanged", 
		self.GeometryChanged_cb, 
		dbus_interface="org.openmoko.Diversity.Object")
	# register a callback for signal PropertyChanged
	self.object_bard.connect_to_signal("PropertyChanged", 
		self.PropertyChanged_cb, 
		dbus_interface="org.openmoko.Diversity.Object")
	
	self.viewport_path = self.world.ViewportAdd(0.0, 0.0, 0.0, 0.0)

    # Viewport
	self.viewport = dbus.Interface(
			self.bus.get_object('org.openmoko.Diversity', 
						self.viewport_path),
			'org.openmoko.Diversity.Viewport'
			)
	
	self.viewport_as_object = dbus.Interface(
			self.bus.get_object('org.openmoko.Diversity', 
						self.viewport_path),
			'org.openmoko.Diversity.Object'
			)

	self.setRadius(r)

 def updateViewport(self):
	 x, y = self.geometry_cord
	 # object.GeometrySet (lon, lat, width, height)
	 (lon, lat, width, height) = (x - self.radius, y - self.radius, 
         self.radius * 2, self.radius * 2)
	 self.viewport_as_object.GeometrySet(lon, lat, width, height) 
	 print 'updateViewport (lon, lat, width, height)',lon, lat, width, height 

 def getTagDescription(self,path):
	self.tag = dbus.Interface(self.bus.get_object('org.openmoko.Diversity', 
                              path),'org.openmoko.Diversity.Tag')
	return self.tag.Get("description")
         
 def setRadius(self, r):
	 self.radius = r
	 print 'self.radius = ', self.radius
	 self.updateViewport()

 def getDiversityObject(self,path):
         self.sobject = dbus.Interface(
       		     self.bus.get_object('org.openmoko.Diversity', path),
		     'org.openmoko.Diversity.Object'
		     )
         return self.sobject

 def getNEOME(self):
	 return self.geometry_cord


 def startViewport(self):
	 self.viewport.Start()

 def getViewportListObjects(self):
	 return self.viewport.ListObjects()
                                                                      
 def removeOneTag(self,tag_path):                                                   
	 res = self.world.TagRemove(tag_path)
	 return res
	 
 def GeometryChanged_cb(self,lon,lat,width,height):
	print 'oh! GeometryChanged_cb() here!',lon,lat,width,height
	self.geometry_cord = (lon, lat)
	self.updateViewport()
	
 def PropertyChanged_cb(self,name,value):
	print 'oh! PropertyChanged_cb() here!', name,value

